﻿using BCCommon;
using BCData.TMS.Driver.DriverLearnDatum;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.Driver.DriverLearnDatum;
using BCEntity.TMS.Driver.DriverLearnDatum;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace BCService.TMS.Driver.DriverLearnDatum
{
    public class DriverLearnDatumAnswerService : IDriverLearnDatumAnswerService
    {
        protected readonly IDriverLearnDatumAnswerData datumAnswerData;
        protected readonly IRedisService redisService;
        protected readonly string entityName = "DriverLearnDatumAnswerEntity";

        public DriverLearnDatumAnswerService(IDriverLearnDatumAnswerData datumAnswerData,
            IRedisService redisService)
        {
            this.datumAnswerData = datumAnswerData;
            this.redisService = redisService;
        }

        public int Add(DriverLearnDatumAnswerRequestDto dto)
        {
            var entity = dto.As<DriverLearnDatumAnswerEntity>();
            return datumAnswerData.InsertAsync(entity).GetAwaiter().GetResult();
        }

        public void Delete(int datumAnswerId)
        {
            bool result = datumAnswerData.DeleteAsync(datumAnswerId).GetAwaiter().GetResult();
            if (result)
            {
                string key = string.Format("{0}@DatumAnswerId:{1}", entityName, datumAnswerId);
                if (this.redisService.Exists(key))
                {
                    this.redisService.Delete(key);
                }
            }
        }

        public DriverLearnDatumAnswerDto GetDriverLearnDatumAnswerById(int datumAnswerId)
        {
            string key = string.Format("{0}@DatumAnswerId:{1}", entityName, datumAnswerId);

            var entity = this.redisService.TryGet(key, () => this.datumAnswerData.GetDriverLearnDatumAnswerByIdAsync(datumAnswerId)).GetAwaiter().GetResult();

            return entity.As<DriverLearnDatumAnswerDto>();
        }

        public PageableList<DriverLearnDatumAnswerDto> GetDriverLearnDatumAnswers(int pageIndex, int pageSize, string answerContent = null)
        {
            var queryResult = datumAnswerData.GetDriverLearnDatumAnswersAsync(pageIndex, pageSize, answerContent).GetAwaiter().GetResult();

            return new PageableList<DriverLearnDatumAnswerDto>
            {
                Items = queryResult.Item1.As<IEnumerable<DriverLearnDatumAnswerDto>>(),
                Count = queryResult.Item2,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }

        public DriverLearnDatumAnswerDto Update(AdminDto admin, int datumAnswerId, DriverLearnDatumAnswerRequestDto dto)
        {
            string key = string.Format("{0}@DatumAnswerId:{1}", entityName, datumAnswerId);
            if (!datumAnswerData.CheckExistsByIdAsync(datumAnswerId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("学习资料答案不存在。");
            }
            var entity = new DriverLearnDatumAnswerEntity
            {
                DatumAnswerId = datumAnswerId,
                AnswerContent = dto.AnswerContent,
                ModifyAdminId = admin.AdminId,
                ModifyDateTime = DateTime.Now
            };
            var result = datumAnswerData.UpdateAsync(entity).GetAwaiter().GetResult();
            if (result != null)
            {
                this.redisService.Delete(key);
            }
            return result.As<DriverLearnDatumAnswerDto>();
        }
    }
}
